Fix TestScheduleStop: replace cron.FuncJob injection with real *Job lifecycle#1955
Merged
Fix TestScheduleStop: replace cron.FuncJob injection with real *Job lifecycle#1955
Conversation
…b injection Agent-Logs-Url: https://github.com/dkron-io/dkron/sessions/80ec29a0-2d29-4f9e-807b-9a3c72a4a8f4 Co-authored-by: vcastellm <47026+vcastellm@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix CI failure in job 69059842947 due to TestScheduleStop
Fix TestScheduleStop: replace cron.FuncJob injection with real *Job lifecycle
Mar 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TestScheduleStopwas manually injecting acron.FuncJobviasched.Cron.AddFunc()and directly settingsched.started = true. Whensched.Start()was called,ClearCron()failed the*Jobtype assertion on theFuncJob, emitting an error log that caused CI to exit 1.Proposed changes
sched.Cron = ...,sched.started = true,sched.Cron.AddFunc(...)) with a propersched.Start([]*Job{testJob1}, &Agent{})call to establish the started state through the public API.@every 10mschedule instead of@every 2sto prevent the job from actually firing during the test — avoids a niljob.loggerpanic that occurs whenJob.Run()executes beforeGetEntryJob()has set the logger.fmt,github.com/distribworks/dkron/v4/extcron,github.com/robfig/cron/v3.The test still validates the full scheduler lifecycle:
Start()succeedsStart()returns an error (already started)<-sched.Stop().Done(),Start()succeeds againStop()leaves the scheduler stoppedTypes of changes
Original prompt
Implement a fix for CI failing job 69059842947 in repository
dkron-io/dkron.Context
The failure is caused by test
TestScheduleStopindkron/scheduler_test.goinjecting acron.FuncJobusingsched.Cron.AddFunc(...)and manually setting scheduler internal state. Duringsched.Start(...),ClearCron()indkron/scheduler.goiterates cron entries and expects*Job, so it logs:scheduler: Failed to cast job to *Job found type cron.FuncJob and removing itThe CI job exits with code 1 after these error logs.
Required Changes
dkron/scheduler_test.goTestScheduleStopto avoid adding rawcron.FuncJobentries.sched.Cron = ...,sched.started = true) in this test.sched.Start(...)with a real*Job, then verify a secondStart(...)call errors when already started.Startreturns error if scheduler already started.Stop()completes,Startsucceeds again.dkron/scheduler_test.go(e.g.,fmt,extcron,cron) if no longer needed.Acceptance Criteria
Failed to cast job to *Job found type cron.FuncJobis emitted byTestScheduleStop.go testfor packagedkronpasses.Suggested Implementation Outline
In
TestScheduleStop:sched := NewScheduler(log).testJob1as existing job fixture.require.NoError(t, sched.Start([]*Job{testJob1}, &Agent{})).assert.Error(t, sched.Start([]*Job{testJob1}, &Agent{})).<-sched.Stop().Done()).assert.NoError(t, sched.Start([]*Job{testJob1}, &Agent{})).sched.Stop()andassert.False(t, sched.Started()).The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Please find a solution for failing job 69059842947. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.
Assistant: Let me fetch the job logs to investigate the failure.
Assistant: The logs mention failing tests but don't show the specific test failure output clearly. Let me fetch the test file and scheduler file to identify the root cause of the
scheduler: Failed to cast job to *Job found type cron.FuncJoberror, which appears to be the key issue.Assistant: ## Root Cause
The key error from the logs is:
This is triggered in
ClearCron()by theTestScheduleStoptest. The test deliberately adds a rawcron.FuncJob(viasched.Cron.AddFunc(...)) before callingsched.Start(), which internally callsClearCron(). WhenClearCron()iterates over entries and encounters theFuncJob, the type assertion to*Jobfails — logging the error and removing the entry.This is expected behavior in production, but the test is causing the error log to be emitted during the test run, which causes the CI job to fail (exit code 1) due to error output being treated as a test failure.
Solution
The
TestScheduleStoptest should avoid adding a rawcron.FuncJobdirectly to the cron instance, or should restructure the test to not trigger the error path inClearCron. The intent of the test appears to be verifying thatStartfails when the scheduler is already started — theAddFuncis only used to simulate a running cron.Replace the raw
AddFunc+ manual state manipulation with a proper firstStart()call so the scheduler is started cleanly:Why this fixes it
sched.Cron.AddFunc(...)injects acron.FuncJob*Jobinstances are added viasched.Start()ClearCron()hits the non-*Jobtype cast, logs an errorClearCron()only sees*Jobentries, no error loggedThis pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.